package cn.hd01.repository;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import cn.hd01.repository.entity.BookRecord;

@Repository
public interface BookRecordRepository extends JpaRepository<BookRecord, Integer> {
	Page<BookRecord> findAll(Specification<BookRecord> spec, Pageable pageable);
	
	List<BookRecord> findAll(Specification<BookRecord> spec);
	
	@Modifying
	@Transactional
	@Query(value = "update BookRecord set status =?1, update_time = now() where id=?2")
	int updateBookRecordStatus(String status, Integer id);
	
	@Query("SELECT id, distributorId, customerId, customerName, productId, productName, bookDate, remark, status, createTime, updateTime FROM BookRecord WHERE book_date >= NOW() AND book_date <= ?1 and status = 1")
	List<BookRecord> findSmsRecord(String time);
	
	@Query(value="SELECT count(1) FROM book_record WHERE book_date >= NOW() and customer_id = ?1", nativeQuery=true)
	Integer countRecord(Integer customerId);
}
